import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import envConfig from '../config/env';
import { AppConfigService } from '../config/config.service';
import { ConfigService, ConfigModule } from '@nestjs/config';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { User } from './user/entities/user.entity';
import { Permission } from './permission/entities/permission.entity';
import { OperationLog } from './log/entities/log.entity';
import { Role } from './role/entities/role.entity';
import { AgentManagModule } from './agent_manag/agent_manag.module';
import { RoleModule } from './role/role.module';
import { PermissionModule } from './permission/permission.module';
import { CacheModule } from './cache/cache.module';
import { LogModule } from './log/log.module';
import { OperationLogInterceptor } from './common/interceptor/log/log.interceptor';
import { GatewaysModule } from './gateways/gateways.module';
@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true, // 设置为全局
      envFilePath: [envConfig.path],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        type: 'mysql', // 数据库类型
        entities: [User, Permission, Role, OperationLog], // 数据表实体
        host: configService.get('DB_HOST', 'localhost'), // 主机，默认为localhost
        port: configService.get<number>('DB_PORT', 3306), // 端口号
        username: configService.get('DB_USER', 'root'), // 用户名
        password: configService.get('DB_PASSWD', 'root11'), // 密码
        database: configService.get('DB_DATABASE', 'crawler'), //数据库名
        timezone: '+08:00', //服务器上配置的时区
        synchronize: true, //根据实体自动创建数据库表， 生产环境建议关闭
        // logging: ['query', 'error'], // 打印查询和错误日志
      }),
    }),
    AuthModule,
    PermissionModule,
    UserModule,
    AgentManagModule,
    RoleModule,
    CacheModule,
    LogModule,
    GatewaysModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    AppConfigService,
    { provide: APP_INTERCEPTOR, useClass: OperationLogInterceptor },
  ],
})
export class AppModule {}
